package com.rc.evcharger.common.type;


import com.rc.evcharger.common.annotation.ByteLength;

import java.lang.reflect.Field;
import java.math.BigDecimal;

public abstract class BaseType implements DataType {
    int length;

    @Override
    public Object getValue(String dataStr) {
        return null;
    }

    private void createFrame(StringBuffer sb, Field field) throws Exception {
        if (field.getName().equals("length") || field.get(this) == null) {
            return;
        }
        //如果子类执行该自定义方法就跳过下页面，进行某个字段自定义构造帧格式数据
        if (customField(field, sb)) {
            return;
        }
        if (field.getType().equals(String.class)) {
            String value = field.get(this).toString();
            if (value.length() < 2)
                sb.append("0");
            sb.append(value).append(SPLIT);
        }
        if (field.getType().equals(BigDecimal.class)) {
            ByteLength bl = field.getAnnotation(ByteLength.class);
            BigDecimal value = (BigDecimal) field.get(this);
            //根据倍率扩大数值
            value = value.multiply(new BigDecimal(Math.pow(10, bl.decimal())));
            //value=value.setScale(bl.decimal(), BigDecimal.ROUND_DOWN);
            String str = String.valueOf(value.intValue());
            //不足长度高位补0
            int len = bl.length() * 2 - str.length();
            for (int i = 0; i < len; i++) {
                str = "0" + str;
            }
            //将数据进行低位在前高位在后    1234  34 21
            for (int i = bl.length(); i > 0; i--) {
                sb.append(str.substring((i - 1) * 2, i * 2)).append(SPLIT);
            }
        }
        if (field.getType().equals(String[].class)) {
            String[] value = (String[]) field.get(this);
            for (String str : value) {
                sb.append(str).append(SPLIT);
            }
        }
        if (field.getType().equals(Integer.class)) {
            Integer value = (Integer) field.get(this);
            String str = Integer.toHexString(value).toUpperCase();
            if (str.length() < 2) {
                sb.append("0");
            }
            sb.append(str).append(SPLIT);
        }
    }

    /***
     * 通过反射构造帧格式数据
     */
    @Override
    public String createFrame() {
        StringBuffer sb = new StringBuffer();
        Field[] fields = this.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                createFrame(sb, field);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        String value = sb.toString().trim();
        this.length = value.split(SPLIT).length;
        return value;
    }

    /**
     * 将数据长度变成16进制
     */
    @Override
    public String getLength() {
        String hex = Integer.toHexString(length);
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        hex = hex.toUpperCase();
        return hex + SPLIT + "00";
    }

    /**
     * 如果某些字段转变数据的方式不一样可以实现这方法
     *
     * @param field
     * @param sb
     * @return
     */
    public abstract boolean customField(Field field, StringBuffer sb);
}
